package com.qf.fmall2203.odder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.qf.fmall2203.index.entity.Product;
import com.qf.fmall2203.index.entity.ProductImg;
import com.qf.fmall2203.index.productsku.entity.ProductSkus;
import com.qf.fmall2203.index.productsku.service.IProductSkuService;
import com.qf.fmall2203.index.service.ProductImgService;
import com.qf.fmall2203.index.service.ProductService;
import com.qf.fmall2203.odder.entity.ShoppingCart;
import com.qf.fmall2203.odder.mapper.ShoppingCartMapper;
import com.qf.fmall2203.odder.service.IShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.fmall2203.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 购物车  服务实现类
 * </p>
 *
 * @author ljc
 * @since 2023-02-16
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {

    @Autowired
    ProductImgService productImgService;
    @Autowired
    ProductService productService;
    @Autowired
    IProductSkuService ProductSkuService;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVo listShopping(Integer userId) {

        List<ShoppingCart> shoppingCarts = list(new QueryWrapper<ShoppingCart>().eq("user_id", userId));

        List<String> productsid = shoppingCarts.stream().map(o -> o.getProductId()).collect(Collectors.toList());

        Map<String, List<ProductImg>> item_id = productImgService.list(new QueryWrapper<ProductImg>().in("item_id", productsid))
                .stream().collect(Collectors.groupingBy(ProductImg::getItemId));

        Map<String, Product> product_id = productService.list(new QueryWrapper<Product>().in("product_id", productsid))
                .stream().collect(Collectors.toMap(p -> p.getProductId(), p -> p));

        Map<String, List<ProductSkus>> productSkus = ProductSkuService.list(new QueryWrapper<ProductSkus>().in("product_id", productsid))
                .stream().collect(Collectors.groupingBy(ProductSkus::getProductId));



        for (ShoppingCart shoppingCart : shoppingCarts) {

            List<ProductImg> productImgs = item_id.get(shoppingCart.getProductId());

            for (ProductImg productImg : productImgs) {
                String url = productImg.getUrl();

                shoppingCart.setProductImg(url);
            }

            Product product = product_id.get(shoppingCart.getProductId());

            shoppingCart.setProductName(product.getProductName());

            List<ProductSkus> productSkusPId = productSkus.get(shoppingCart.getProductId());

            for (ProductSkus skus : productSkusPId) {
                if (shoppingCart.getSkuId().equals(skus.getSkuId())) {
                    shoppingCart.setSkuName(skus.getSkuName());

                    shoppingCart.setSellPrice(skus.getSellPrice());

                    shoppingCart.setStock(skus.getStock());

                    shoppingCart.setOriginalPrice(skus.getOriginalPrice());
                }
            }

        }
        return ResultVo.ok(shoppingCarts);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo updateNum(Integer cart_id, String cartNum) {

        ShoppingCart cart_id1 = getOne(new QueryWrapper<ShoppingCart>().eq("cart_id", cart_id));

        update(cart_id1, new UpdateWrapper<ShoppingCart>().set("cart_num", cartNum).eq("cart_id", cart_id));

        return ResultVo.ok();
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVo listByCIds(String cids) {

        List<String> list = Arrays.asList(cids.split(","));
        List<ShoppingCart> list1 = new ArrayList<>();

        for (String s : list) {
            ShoppingCart byId = getById(Integer.valueOf(s));
            List<ProductImg> product_id = productImgService.list(new QueryWrapper<ProductImg>().eq("item_id", byId.getProductId()));

            Map<String, Product> product_id1 = productService.list(new QueryWrapper<Product>().in("product_id", byId.getProductId()))
                    .stream().collect(Collectors.toMap(p -> p.getProductId(), p -> p));

            Map<String, List<ProductSkus>> productSkus = ProductSkuService.list(new QueryWrapper<ProductSkus>().in("product_id", byId.getProductId()))
                    .stream().collect(Collectors.groupingBy(ProductSkus::getProductId));

            Product product= null;
            if((product=product_id1.get(byId.getProductId()))!=null){
                byId.setProductName(product.getProductName());
            }

            List<ProductSkus> productSkuses = productSkus.get(byId.getProductId());

            List<ProductSkus> productSkusStream = productSkuses.stream().filter(productSkus1 -> productSkus1.getSkuId().equals(byId.getSkuId()))
                    .collect(Collectors.toList());

            if (productSkusStream!=null&&productSkusStream.size()!=0){

                for (ProductSkus skus : productSkusStream) {
                    byId.setSkuName(skus.getSkuName());

                    byId.setSellPrice(skus.getSellPrice());

                    byId.setStock(skus.getStock());

                    byId.setOriginalPrice(skus.getOriginalPrice());

                }
            }

            for (ProductImg productImg : product_id) {
                byId.setProductImg(productImg.getUrl());
            }

            list1.add(byId);
        }

        return ResultVo.ok(list1);
    }
}
